const fs = require('fs')
process.on('message', (msg) => {
    const folder = `upload-tool(node)/static/${msg.fileName.split('.')[0]}`
    const scope = [msg.startPage, msg.endPage]
    fs.mkdirSync(`../${folder}/数据整理`, { recursive: true })
    for (let i = scope[0]; i < scope[1]; i++) {
        process.send({ all: scope[1] - scope[0], current: (i - scope[0]) || 1, progress: true })//将进度传给封装的命令文件
        for (let page = 1; page < 5; page++) {
            let data = []
            try {
                const str = fs.readFileSync(`../${folder}/data/${i}/data_${i}_${page}.json`, 'utf-8')?.slice(0, -1)?.split(`_${page}=`)[1]
                data = JSON.parse(str)
            } catch (e) { }
            // 判断是否发送文件
            const flat = data.find(v => v?.Obj?.Contents?.[0]?.Type === 'Attachment' && (v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xlsx') || v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xls')))
            if (!flat) continue
            // 创建文件夹结构  Way: 0 接收方 1 发送方 2 系统
            let account = data.find(v => v.Obj.Way === 0 && v?.Obj.Sender.AccountFormat)?.Obj.Sender.AccountFormat
            if (!account) account = '文件传输助手'
            const senderFilters = data.map(v => {
                // 筛选发送方文件并复制
                if (v.Obj.Way === 1 && v.Obj?.Contents[0].Type === 'Attachment' && (v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xlsx') || v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xls'))) {
                    fs.mkdirSync(`../${folder}/数据整理/${account}/发送方`, { recursive: true })
                    const oldPath = v.Obj?.Contents?.[0].Value.UIPath.RelativePath.slice(1)
                    const files = fs.readdirSync(`../${folder}/数据整理/${account}/发送方`);
                    const newPath = `../${folder}/数据整理/${account}/发送方/${files.length + 1}.xlsx`;
                    try {
                        fs.copyFileSync('../' + folder + oldPath, newPath);
                    } catch (error) {
                        return
                    }
                    return oldPath
                }
            })
            const accountFilters = data.map(v => {
                // 筛选接收方文件
                if (v.Obj.Way === 0 && v.Obj?.Contents[0].Type === 'Attachment' && (v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xlsx') || v.Obj?.Contents?.[0]?.Value.UIPath.RelativePath.endsWith('.xls'))) {
                    fs.mkdirSync(`../${folder}/数据整理/${account}/接收方`, { recursive: true })
                    const oldPath = v.Obj?.Contents?.[0].Value.UIPath.RelativePath.slice(1)
                    const files2 = fs.readdirSync(`../${folder}/数据整理/${account}/接收方`);
                    const newPath = `../${folder}/数据整理/${account}/接收方/${files2.length + 1}.xlsx`;
                    try {
                        fs.copyFileSync('../' + folder + oldPath, newPath);
                    } catch (error) {
                        return
                    }
                    return oldPath
                }
            })
            // console.log(`发送方页面${i}-${page}/${scope[1]}`)
            // console.log(`接收方页面${i}-${page}/${scope[1]}`)
        }
    }
    process.send({ progress: false })//将进度传给主进程
})